#include "FILEWR.h"

#include <QFile>
#include <QDebug>
#include <QTextStream>
#include <QDateTime>
#include <QErrorMessage>

//#include"./m690/mainwindow.h"

//QList<QDateTime> timelist;
bool FILEWARE::writeLogCSV(QString tag,QString str)
{
    if(tag != "") tag+= "_";
    QString path=QString(".\\user\\registers\\%1%2.txt").arg(tag, QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    QFile file(path);
    if(file.open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text))
    {
        QString string=QString("%1----:%2\n")
                .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
                .arg(str);
        file.write(string.toLatin1());
        file.close();
        return true;
    }
    return false;
}

bool FILEWARE::writeLogCSVThread(QString str)
{
    QString path=QString(".\\system\\%1-Thread.txt").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    QFile file(path);
    if(file.open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text))
    {
        QString string=QString("%1-%2\n")
                .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
                .arg(str);
        file.write(string.toLatin1());
        file.close();
        return true;
    }
    return false;
}

bool FILEWARE::writePositionC(const QString &path, QString str)
{
    QFile file(path);
    if(file.open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text))
    {
        QString string=QString("%1%2\n")
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
                .arg(str);
        file.write(string.toLatin1());
        file.close();
        return true;
    }
    return false;
}

bool FILEWARE::writeWaveLengthC(const QString &path, QString str)
{
//    qDebug()<<QDateTime::currentDateTime().toString("hh:mm:ss.zzz")<<"save:"<<str;

//    QDateTime time = QDateTime::currentDateTime();
//    timelist.append(time);
//    int msc;
//    if (timelist.length() > 22)
//    {
//        for(int i=0;i<20;i++)
//        {
//           msc = timelist[i].msecsTo(timelist[i+1]);
//           qDebug() << "time"+QString::number(i+1,10)+"-time"+QString::number(i,10)+":"<<msc;
//        }
//        msc +=1;
//    }
    QFile file(path);
    if(file.open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text))
    //if(file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QString string=QString("%1%2\n")
//                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
                .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))

                .arg(str);
//        qDebug()<<string;
        file.write(string.toLatin1());
        file.close();
        return true;
    }
    return false;
}

bool FILEWARE::write_csv(const QString &path, QString str)
{
    QFile file(path);
    if(file.open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text))
    {
        QString string=QString("%1\n")
                .arg(str);
        file.write(string.toLatin1());
        file.close();
        return true;
    }
    return false;
}

bool FILEWARE::write_to_txt(const QString &path, QString str)
{
    QFile file(path);
    if(file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        file.write(str.toLatin1());
        file.close();
        return true;
    }
    return false;
}

QStringList FILEWARE::ReadCSV(const QString &path)
{
    QFile csvFile(path);
    QStringList list;
    if(csvFile.open(QIODevice::ReadOnly))
    {
        QTextStream stream(&csvFile);
        while(!stream.atEnd())
        {
            list.push_back(stream.readLine());
        }
        csvFile.close();
    }
    return list;
}

void FILEWARE::error(QString msg, QString type)
{
    QErrorMessage error;
    error.showMessage(msg);
    error.setWindowTitle(type);
    error.setWindowFlags(Qt::WindowStaysOnTopHint);
    error.exec();
    return;
}
